<template>
  <!-- 添加  搜所 -->
  <div class="app-container">
    <!-- 表 -->
    <el-table :data="list">
      <el-table-column
        label="批次库存ID"
        prop="batchesOfInventoryId"
        align="center"
        v-if="false"
      />
      <!--  药品名称id medicinesId -->
      <el-table-column label="诊所ID" prop="hospitalName" align="center" />
      <el-table-column label="供应商ID" prop="supplierName" align="center" />
      <el-table-column
        label="采购单编号"
        prop="batchesOfInventoryNumber"
        align="center"
      />
      <el-table-column label="采购员ID" prop="userName" align="center" />
      <el-table-column
        label="采购总金额"
        prop="batchesOfInventoryTotalMoney"
        align="center"
      />
      <el-table-column
        label="制单日期"
        prop="batchesOfInventoryDate"
        align="center"
      />
      <el-table-column label="审核人员ID" prop="userName2" align="center" />
      <el-table-column
        label="审核日期"
        prop="batchesOfInventoryVerifierDate"
        align="center"
      />

      <el-table-column align="center" label="审核状态">
        <template slot-scope="scope">
          <p v-if="scope.row.batchesOfInventoryStatus == '1'">
            <el-button
              type="success"
              icon="el-icon-check"
              circle
              @click="insert_inventory(scope.row)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-ass"
              circle
              @click="butongguo(scope.row)"
              >Χ</el-button
            >
          </p>

          <p v-if="scope.row.batchesOfInventoryStatus == '2'">
            已通过
            <!-- <el-table-column align="center">
              <template slot-scope="scope">
                <el-button
                  size="20"
                  type="primary"
                  @click="insert_inventory2(scope.row)"
                >
                  再次入库</el-button
                >
              </template>
            </el-table-column> -->
          </p>

          <p v-if="scope.row.batchesOfInventoryStatus == '3'">审核不通过</p>
        </template>
      </el-table-column>

      <el-table-column align="right">
        <template slot-scope="scope">
          <el-button size="20" type="primary" @click="handleselect(scope.row)">
            查看</el-button
          >
        </template>
      </el-table-column>

      <el-table-column align="center">
        <template slot-scope="scope">
          <el-button
            v-show="scope.row.batchesOfInventoryStatus == 3 || scope.row.batchesOfInventoryStatus == 1  ? false : true"
            size="20"
            type="primary"
            @click="insert_inventory2(scope.row)"
          >
            再次入库</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页栏 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[8, 10, 25, 50]"
      :page-size="100"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <!-- ==================================== -->

    <el-dialog :visible.sync="dialogStatus2" customClass="cdialogStatus2_width">
      <!-- 查询数据 -->
      <el-table :data="ticket2" style="width: 100%">
        <el-table-column label="Id" prop="purchaseId" align="center">
        </el-table-column>
        <!--  药品名称id medicinesId -->
        <el-table-column label="药品名称" prop="medicinesName" align="center" />
        <!-- <el-table-column
          label="批次库存ID"
          prop="batchesOfInventoryId"
          align="center"
        /> -->
        <el-table-column label="采购数量" prop="purchaseNum" align="center" />
        <el-table-column label="采购单位" prop="purchaseUnit" align="center" />
        <el-table-column
          label="批发价"
          prop="purchaseTradePrice"
          align="center"
        />
        <el-table-column
          label="处方价"
          prop="purchasePrescriptionPrice"
          align="center"
        />
        <el-table-column
          label="采购批发总额"
          prop="purchaseTradeTotalAmount"
          align="center"
        />
        <el-table-column
          label="采购处方总额"
          prop="purchasePrescriptionTotalAmount"
          align="center"
        />
        <el-table-column label="创建时间" prop="createTime" align="center" />
      </el-table>
    </el-dialog>
  </div>
</template>


<script>
import { DzmHisPurchase_batch_query } from "@/api/api_g";
import { dzmHisPurchase_review_the_drugs } from "@/api/api_g";
import { dzmHisInventory_update } from "@/api/api_g";
import { dzmHisBatchesOfInventory_update_statu } from "@/api/api_g";
import { dzmHisBatchesOfInventory_update_statu_btg } from "@/api/api_g";

export default {
  name: "",
  data() {
    return {
      // 分页后的当前页数据
      list: [],
      //当前数据页的第几页
      currentPage: 1,
      // 总数据条数xx
      total: 0,
      // 每页显示多少行数据,默认为10条
      pagesize: 8,
      // 代表是从后端获取的所有数据
      ticket: [],
      // 控制编辑排版信息对话框
      open: false,

      monitorForm: {},

      // 表单参数
      temp: "",

      // 重置 添加表单
      resetTemp() {
        this.temp = {};
      },

      temp: {},
      dialogFormVisible: false, //新增修改弹出框
      dialogStatus: "", //弹出框 显示隐藏控制
      textMap: {
        create: "新增",
      },

      // ========
      ticket2: [],
      dialogStatus2: false, //弹出查询弹出框

      intsrt_inv: {
        hid: [],
      },
    };
  },
  mounted() {
    this.getDoctors();
  },
  methods: {
    /** 查询 */
    getDoctors() {
      //this.listLoading = true;
      DzmHisPurchase_batch_query().then((response) => {
        // console.log(response.data[0]);
        if (response.code === 200) {
          this.ticket = response.data;
          //获取返回数据的总行数
          this.total = response.data.length;
          // 获取当前页的数据,此方法在下方定义
          this.getDoctorInfo();
        }
      });
    },
    // 此方法用来获取当前页的数据
    getDoctorInfo() {
      // 清空表格的list 数据
      this.list = [];
      // 获取当前页的数据
      for (
        let i = (this.currentPage - 1) * this.pagesize;
        i < this.total;
        i++
      ) {
        // 将便利的数据添加到 list 里面
        this.list.push(this.ticket[i]);
        // 判断是否达到每一页的要求
        if (this.list.length === this.pagesize) break;
      }
    },

    // 分页时修改每页的行数,这里会自动传入一个size
    handleSizeChange(size) {
      // 修改当前每页的数据行数
      this.pagesize = size;
      // 数据重新分页
      this.getDoctorInfo();
    },
    // 调整当前的页码
    handleCurrentChange(pageNumber) {
      // 修改当前的页码
      this.currentPage = pageNumber;
      //数据重新分页
      this.getDoctorInfo();
    },

    //========================================

    handleselect(row) {
      this.ticket2 = [];

      this.dialogStatus2 = true;

      dzmHisPurchase_review_the_drugs(row).then((response) => {
        if (response.code === 200) {
          this.ticket2 = response.data;
        }
      });
    },

    //再次入库
    async insert_inventory2(row) {
      this.$confirm("此操作将药物再次入库, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          var companyId = row.companyId;

          for (var i = 0; i < this.ticket2.length; i++) {
            this.ticket2[i].companyId = companyId;

            dzmHisInventory_update(this.ticket2[i]).then((response) => {
              if (response.code === 200) {
                dzmHisBatchesOfInventory_update_statu(row.batchesOfInventoryId);
                this.getDoctors();
              }
            });
          }
        })
        .then(() => {});
    },

    //判断  是否入库
    insert_inventory(row) {
      var companyId = row.companyId;

      for (var i = 0; i < this.ticket2.length; i++) {
        this.ticket2[i].companyId = companyId;

        dzmHisInventory_update(this.ticket2[i]).then((response) => {
          if (response.code === 200) {
            dzmHisBatchesOfInventory_update_statu(row.batchesOfInventoryId).then(res=>{
             this.getDoctors();
            });
            //this.getDoctors();
          }
        });
      }
    },

    //修改不通过
    butongguo(row) {
      dzmHisBatchesOfInventory_update_statu_btg(row.batchesOfInventoryId).then(res=>{
        this.getDoctors();
      });


    },
  },
};
</script>

  <style>
.cdialogStatus2_width {
  width: 78%;
}
</style>
